package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.IAttendanceStatisticsBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.entity.AttendanceStatistics;
import com.cloudservice.ccs.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attendance-statistics")
public class AttendanceStatisticsController {
    @Autowired
    private IAttendanceStatisticsBiz attendanceStatisticsBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody AttendanceStatistics attendanceStatistics) {
        // 接口问题待讨论：是否应当返回生成的 id？
        attendanceStatisticsBiz.save(attendanceStatistics);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody AttendanceStatistics attendanceStatistics) {
        attendanceStatisticsBiz.update(id, attendanceStatistics);
        AttendanceStatistics result = attendanceStatisticsBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        attendanceStatisticsBiz.partialUpdate(id, fields);
        AttendanceStatistics result = attendanceStatisticsBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        attendanceStatisticsBiz.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        AttendanceStatistics result = attendanceStatisticsBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<AttendanceStatistics> result = attendanceStatisticsBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<AttendanceStatistics>) result));
    }
}
